﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace QLBV
{
    
    public partial class QLT : System.Web.UI.Page
    {
        SqlConnection conn;
            String strCon = "Data Source=HTD-COM;Initial Catalog=QLBV;Integrated Security=True;Pooling=False";
        protected void Page_Load(object sender, EventArgs e)
        {
            Load_Table();
        }
        public void Load_Table()
        {
            String strSelect = "SELECT * FROM Thuoc";
            conn = new SqlConnection(strCon);
            conn.Open();
            SqlDataAdapter adapter = new SqlDataAdapter(strSelect, conn);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            gvQLT.DataSource = dt;
            gvQLT.DataBind();
            conn.Close();
            }
        protected void TextBox4_TextChanged(object sender, EventArgs e)
        {

        }

        protected void gvQLT_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbMsg.Text = "";
            String strThuoc = "Select * from Thuoc where MaThuoc='" + gvQLT.SelectedRow.Cells[1].Text + "'";
           conn.Open();
            SqlDataAdapter da = new SqlDataAdapter(strThuoc, conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "Thuoc");
            txtMT.Text = ds.Tables["Thuoc"].Rows[0][0].ToString();
            txtTT.Text = ds.Tables["Thuoc"].Rows[0][1].ToString();
            txtGT.Text = ds.Tables["Thuoc"].Rows[0][2].ToString();
            txtDV.Text = ds.Tables["Thuoc"].Rows[0][3].ToString();
            txtMoTa.Text = ds.Tables["Thuoc"].Rows[0][4].ToString();
        }

        protected void btTT_Click(object sender, EventArgs e)
        {
              String strInsertThuoc="INSERT INTO Thuoc	(TenThuoc, GiaThuoc,DonVi,MoTa)VALUES ( @tenthuoc,@giathuoc,@donvi,@mota)";

              if (txtTT.Text == "" ||
            txtGT.Text == "" ||
            txtDV.Text == "" ||
            txtMoTa.Text == "")
              {
                  lbMsg.Text = "Vui lòng nhập đầy đủ thông tin ";
              }
              else
              {
                  lbMsg.Text = "";
                  conn.Open();
                  SqlCommand cmd = new SqlCommand(strInsertThuoc, conn);
                  cmd.Parameters.Add("@tenthuoc", txtTT.Text);
                  cmd.Parameters.Add("@giathuoc", txtGT.Text);
                  cmd.Parameters.Add("@donvi", txtDV.Text);
                  cmd.Parameters.Add("@mota", txtMoTa.Text);
                  cmd.ExecuteNonQuery();
                  reset();
                  conn.Close();
                  Load_Table();
              }

        }

        protected void btUpdate_Click(object sender, EventArgs e)
        {
            lbMsg.Text = "";
            String strUpdate = "UPDATE Thuoc SET  TenThuoc=@tenthuoc, GiaThuoc=@giathuoc,DonVi=@donvi,MoTa=@mota WHERE MaThuoc = '" + txtMT.Text + "'";
            if (txtTT.Text == "" ||
            txtGT.Text == "" ||
            txtDV.Text == "" ||
            txtMoTa.Text == "")
            {
                lbMsg.Text = "Vui lòng nhập đầy đủ thông tin ";
            }
            else
            {
                conn.Open();
                //update ten thuoc
                SqlCommand cmd = new SqlCommand(strUpdate, conn);
                cmd.Parameters.Add("@tenthuoc", txtTT.Text);
                cmd.Parameters.Add("@giathuoc", txtGT.Text);
                cmd.Parameters.Add("@donvi", txtDV.Text);
                cmd.Parameters.Add("@mota", txtMoTa.Text);
                cmd.ExecuteNonQuery();
                conn.Close();
                Load_Table();
                reset();
            }
        }

        protected void btDelete_Click(object sender, EventArgs e){
          lbMsg.Text = "";
            if (txtMT.Text == "") lbMsg.Text = "Vui lòng chọn bệnh nhân đề xóa!";
            else
                {
                        String strDelete = "DELETE FROM Thuoc WHERE MaThuoc = '" + txtMT.Text + "'";
                        conn.Open();
                        SqlCommand cmd = new SqlCommand(strDelete, conn);
                        cmd.ExecuteNonQuery();
                        reset();
                        conn.Close();
                        Load_Table();
                        reset();
                }
        }

        protected void btReset_Click(object sender, EventArgs e)
        {
            reset();
            
        }
        public void reset()
        {
            lbMsg.Text = "";
            txtMT.Text = "";
            txtTT.Text = "";
            txtGT.Text = "";
            txtDV.Text = "";
            txtMoTa.Text = "";
        }

        protected void txtMT_TextChanged(object sender, EventArgs e)
        {

        }

        protected void gvQLT_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvQLT.PageIndex = e.NewPageIndex;
            gvQLT.DataBind();

        }

        

        

       
    }
}